package com.example.gestureprac

import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.constraintlayout.widget.ConstraintLayout.LayoutParams

class SlideGestureActivity : AppCompatActivity() {

    private lateinit var container: ConstraintLayout
    private lateinit var dragView: View
    private lateinit var swipeView: TextView
    private lateinit var pageIndicator: TextView
    private var currentPage = 0
    private val totalPages = 3
    private var initialX = 0f
    private var initialY = 0f
    private var startSwipeX = 0f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_slide_gesture)

        container = findViewById(R.id.slide_gesture_container)
        dragView = findViewById(R.id.drag_view)
        swipeView = findViewById(R.id.swipe_view)
        pageIndicator = findViewById(R.id.page_indicator)
        val backButton = findViewById<TextView>(R.id.back_button)
        val titleText = findViewById<TextView>(R.id.title_text)
        val dragExampleText = findViewById<TextView>(R.id.drag_example_text)
        val swipeExampleText = findViewById<TextView>(R.id.swipe_example_text)

        // 初始化拖动手势
        setupDragGesture()

        // 初始化滑动手势
        setupSwipeGesture()

        // 设置返回按钮点击事件
        backButton.setOnClickListener {
            finish()
        }

        // 设置初始页面
        updatePageContent()
    }

    /**
     * 设置拖动手势
     */
    private fun setupDragGesture() {
        dragView.setOnTouchListener(object : OnTouchListener {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        // 记录初始触摸位置
                        initialX = event.rawX - v.x
                        initialY = event.rawY - v.y
                    }
                    MotionEvent.ACTION_MOVE -> {
                        // 计算新位置
                        val newX = event.rawX - initialX
                        val newY = event.rawY - initialY

                        // 确保视图不会移出容器边界
                        val maxX = container.width - v.width
                        val maxY = container.height - v.height
                        val boundedX = newX.coerceIn(0f, maxX.toFloat())
                        val boundedY = newY.coerceIn(0f, maxY.toFloat())

                        // 更新视图位置
                        v.x = boundedX
                        v.y = boundedY
                    }
                }
                return true
            }
        })
    }

    /**
     * 设置滑动手势
     */
    private fun setupSwipeGesture() {
        swipeView.setOnTouchListener(object : OnTouchListener {
            override fun onTouch(v: View, event: MotionEvent): Boolean {
                when (event.action) {
                    MotionEvent.ACTION_DOWN -> {
                        startSwipeX = event.x
                    }
                    MotionEvent.ACTION_UP -> {
                        val endSwipeX = event.x
                        val swipeDistance = endSwipeX - startSwipeX

                        // 检测滑动距离是否足够大
                        if (swipeDistance > 50) {
                            // 向右滑动，显示上一页
                            if (currentPage > 0) {
                                currentPage--
                                slideAnimation(-1)
                                updatePageContent()
                            }
                        } else if (swipeDistance < -50) {
                            // 向左滑动，显示下一页
                            if (currentPage < totalPages - 1) {
                                currentPage++
                                slideAnimation(1)
                                updatePageContent()
                            }
                        }
                    }
                }
                return true
            }
        })
    }

    /**
     * 滑动动画
     */
    private fun slideAnimation(direction: Int) {
        val animation = TranslateAnimation(
            0f,
            (direction * swipeView.width * 0.5f).toFloat(),
            0f,
            0f
        )
        animation.duration = 200
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {}
            
            override fun onAnimationEnd(animation: Animation) {
                swipeView.clearAnimation()
            }
            
            override fun onAnimationRepeat(animation: Animation) {}
        })
        swipeView.startAnimation(animation)
    }

    /**
     * 更新页面内容
     */
    private fun updatePageContent() {
        when (currentPage) {
            0 -> {
                swipeView.text = "第一页\n向左滑动查看下一页"
                swipeView.setBackgroundColor(resources.getColor(android.R.color.holo_blue_light))
            }
            1 -> {
                swipeView.text = "第二页\n向左滑动查看下一页\n向右滑动返回上一页"
                swipeView.setBackgroundColor(resources.getColor(android.R.color.holo_green_light))
            }
            2 -> {
                swipeView.text = "第三页\n向右滑动返回上一页"
                swipeView.setBackgroundColor(resources.getColor(android.R.color.holo_orange_light))
            }
        }
        pageIndicator.text = "第 ${currentPage + 1} / $totalPages 页"
    }
}